package com.vue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vue.bean.R;
import com.vue.entity.UserEntity;
import com.vue.entity.po.LoginPO;
import com.vue.entity.po.UserPO;
import com.vue.mapper.UserMapper;
import com.vue.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ServiceImpl实现了IService，提供了IService中基础功能的实现
 * 若ServiceImpl无法满足业务需求，则可以使用自定的UserService定义方法，并在实现类中实现
 * @author: wrj
 * @since: 2023/6/8 17:55
 * @description:
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {


    @Override
    public R login(LoginPO po) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(po.getName())){
            queryWrapper.lambda().eq(UserEntity::getName, po.getName());
        }
        if (!ObjectUtils.isEmpty(po.getPassword())){
            queryWrapper.lambda().eq(UserEntity::getPassword, po.getPassword());
        }
        UserEntity userEntity = baseMapper.selectOne(queryWrapper);
        if (userEntity != null) {
            return R.data(userEntity);
        }else {
            return R.fail("用户名或密码错误");
        }
    }


    @Override
    public IPage<UserEntity> selectPage(UserPO po, Integer pageNum, Integer pageSize) {
        IPage iPage = new Page(pageNum, pageSize);
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(po.getName())){
            queryWrapper.lambda().like(UserEntity::getName, po.getName());
        }
        if (!ObjectUtils.isEmpty(po.getRealName())){
            queryWrapper.lambda().like(UserEntity::getRealName, po.getRealName());
        }
        if (!ObjectUtils.isEmpty(po.getSex())){
            queryWrapper.lambda().eq(UserEntity::getSex, po.getSex());
        }
        if (!ObjectUtils.isEmpty(po.getEmail())){
            queryWrapper.lambda().like(UserEntity::getEmail, po.getEmail());
        }
        return baseMapper.selectPage(iPage, queryWrapper);
    }

    @Override
    public List<UserEntity> selectList(UserPO po) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(po.getName())){
            queryWrapper.lambda().like(UserEntity::getName, po.getName());
        }
        if (!ObjectUtils.isEmpty(po.getRealName())){
            queryWrapper.lambda().like(UserEntity::getRealName, po.getRealName());
        }
        if (!ObjectUtils.isEmpty(po.getSex())){
            queryWrapper.lambda().eq(UserEntity::getSex, po.getSex());
        }
        if (!ObjectUtils.isEmpty(po.getEmail())){
            queryWrapper.lambda().like(UserEntity::getEmail, po.getEmail());
        }
//        queryWrapper.lambda().orderByDesc(UserEntity::getName);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public R addOrEditUser(UserEntity po) {
        if (po.getId() == null){
            baseMapper.insert(po);
        }else {
            baseMapper.updateById(po);
        }
        return R.data(po);
    }

    @Override
    public R deleteById(Long id) {
        UserEntity entity = baseMapper.selectById(id);
        if (entity == null){
            return R.fail("用户不存在");
        }
        if(entity.getId() == 1L
            || entity.getId() == 2L){
            return R.fail("老板账号，可不敢删");
        }
        removeById(id);
        return R.status(true);
    }

    @Override
    public R selectById(Long id) {
        return R.data(getById(id));
    }

    @Override
    public R batchInsert(Integer count) {
        List<UserEntity> list = new ArrayList();
        for (int i=0;i<count;i++){
            UserEntity entity = new UserEntity();
            entity.setName("test");
            entity.setRealName("测试姓名");
            entity.setPassword("123456");
            entity.setAge(30);
            entity.setSex(1);
            Integer a = 50001+i;
            entity.setEmail(a.toString());
            list.add(entity);
        }
        boolean b = saveBatch(list);
        return R.status(b);
    }

}


